import { Result } from 'antd';
import React, { Component, ErrorInfo } from 'react';

export class ErrorBoundary extends Component<{ children: React.ReactNode }, { hasError: boolean; errorInfo: string }> {
  state = { hasError: false, errorInfo: '' };

  static getDerivedStateFromError(error: Error) {
    return { hasError: true, errorInfo: error.message };
  }

  componentDidCatch(error: any, errorInfo: ErrorInfo) {
    console.log(error, errorInfo);
  }

  render() {
    if (this.state.hasError) {
      return <Result status="error" title="Something getting wrong" extra={this.state.errorInfo} />;
    }
    return this.props.children;
  }
}
